// pages/flight/index.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_data: {
      flight_date: '航班出发日期',
      flight_date_end: '航班到达日期',
      flight_start: '出发时间',
      flight_end: '到达时间',
    },
    disabled: 'disabled',
    flag: 0,
    val: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var date = new Date(),
      y = date.getFullYear(),
      m = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1,
      d = date.getDate(),
      h = date.getHours(),
      ms = date.getMinutes(),
      today = y + '-' + m + '-' + d,
      now = h + ':' + ms

    this.setData({today: today, start_time: now, now: now})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  selectDate(e) {
    var _this = this,
        value = e.detail.value,
        index = e.currentTarget.dataset.index,
        end ,start,
        data = {}

    if(index == 0) {
      if(value != _this.data.today) {      //是不是今天 是今天加上时间限制 不是今天不加
        data['start_time'] = '00:00'
      }else {
        data['start_time'] = _this.data.now
      }
      data['input_data.flight_date'] = value
      data['color.color_1'] = 'checked'
      data['end_date'] = value
    }else {
      data['input_data.flight_date_end'] = value
      data['color.color_4'] = 'checked'
    }
    var reg = /^\d{4}-\d{1,2}-\d{1,2}$/
    _this.setData(data)
    start = _this.data.input_data.flight_date
    end = _this.data.input_data.flight_date_end
    if (reg.test(start) && reg.test(end)){
      data['flag'] = 1
    }
    else data['flag'] = 0

    _this.setData({ flag: data['flag'] })
    _this.checkDate(_this);
  },
  selectTime(e) {
    var _this = this,
        value = e.detail.value,
        index = e.currentTarget.dataset.index,
        reg = /^\d{2}:\d{2}$/,
        data = {},
        start, end
    
    //判断时间
    if(!reg.test(value)) {
      var ap = value.substring(0, 2),
          time = value.substring(2, value.length),
          explode_time = time.split(':')
      if(ap == '下午') {
        explode_time[0] = parseInt(explode_time[0]) + 12
      }else if(ap == '上午') {
        if ( explode_time[0] < 10 ) {
          explode_time[0] = '0' + explode_time[0]
        }
      }
      value = explode_time[0] + ':' + explode_time[1]
      console.info(value)
    }

    switch(index) {
      case '0':
        data['input_data.flight_start'] = value
        data['color.color_2'] = 'checked'
        //如果日期时同一天，加上时间限制，不是则不加
        if(_this.data.input_data.flight_date == _this.data.input_data.flight_date_end) {
          data['end_time'] = value
        }else {
          data['end_time'] = '00:00'
        }
        break;
      case '1':
        data['input_data.flight_end'] = value
        data['color.color_3'] = 'checked'
    }
    _this.setData(data)
    start = _this.data.input_data.flight_start 
    end = _this.data.input_data.flight_end 
    if (reg.test(start) && reg.test(end)) {
      data['flag'] = 1
    }
    else data['flag'] = 0
    
    _this.setData({ flag: data['flag']})
    _this.checkDate(_this);
  },
  Check(e){
    var _this = this,
        value = e.detail.value,
        id = e.currentTarget.id,
        flag = _this.data.flag,
        data = {},
        val = {}

    data['val[' + id + ']'] = value
    
    _this.setData(data)
    for(var i=0; i<10; i++){
      val[i] = _this.data.val[i]
      if (val[i] != '' && val[i] != undefined && flag == 1) data['disabled'] = ''
      else data['disabled'] = 'disabled'
    }
    _this.setData({disabled: data['disabled']}) 
  },
  checkDate(_this) {
    if (_this.data.input_data.flight_date > _this.data.input_data.flight_date_end) {
      _this.setData({
        'input_data.flight_date_end': '航班到达日期', 'input_data.flight_end': '到达时间',
        'color.color_4': '', 'color.color_3': ''
      })
    } 
  },
  submit(e) {
    var _this = this,
        data = e.detail.value

    data['flt_seller_token'] = app.globalData.userInfo.user_token 
    wx.showLoading({
      title: '加载中'
    })

    req.request(
      api.addFlightInfo,
      {method: 'POST', data: data},
      function(res) {
        wx.hideLoading();
        res = res.data

        wx.showToast({
          title: res.msg,
          icon: 'none'
        })

        if(res.status == 1) {
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 3000)
        }
      }
    );
  }
})